package com.kermitye.studentsys;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ImageView mIvAdd;
    RecyclerView mRv;
    StudentAdapter mAdapter;
    List<Student> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        mIvAdd = findViewById(R.id.am_iv_add);
        mRv = findViewById(R.id.am_rv_studnets);
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mDatas = new ArrayList<>();
        mAdapter = new StudentAdapter(this, mDatas);
        mRv.setAdapter(mAdapter);
        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtra("type", "add");
                startActivity(intent);
            }
        });

        mAdapter.setOnItemClickListener(new StudentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Student item) {
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtra("type", "edit");
                intent.putExtra("data", item);
                startActivity(intent);
            }
        });

        mAdapter.setOnItemLongListener(new StudentAdapter.OnItemLongListener() {
            @Override
            public void onItemLongClick(int position, Student item) {
                showDelDialog(position, item);
            }
        });

    }

    private void initData() {
        MyService.instance.getData(new GetDataListener() {
            @Override
            public void onFinish(final List<Student> data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.clear();
                        mDatas.addAll(data);
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }


    private void showDelDialog(final int position, final Student student) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("是否删除学生:" + student.name);
        builder.setCancelable(true);

        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mDatas.remove(position);
                MyService.instance.dbManager.delete(student);
                mAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}
